<%@ page buffer="16kb" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib uri="http://java.sun.com/jstl/core" prefix="c" %>  

<%@page import="twitter4j.Status"%>

<jsp:useBean id="twitterBean" class="ar.edu.itba.twiteros.bean.TwitterosBean" scope="page"/>

<script type="text/javascript" src="http://twiteros.appspot.com/static/js/animation.js"></script>

<script>
function checkForm(form) {
	var params=form.serialize(); 
	var msg = document.getElementById('error_message');
	if (params.message.length>0 && params.message.length<140)
	{ 
		//Animation(elm).to('height', '0px').to('opacity', 0).hide().go();
		//msg.setStyle('visibility', 'hidden');
		msg.setStyle('display', 'none');
		return true; 
	}else
	{
		//Animation(elm).to('height', 'auto').to('opacity', 1).show().go();
		//msg.setStyle('visibility', 'visible');  
		msg.setStyle('display', 'block');
		return false;
	}
			
}

</script>


<% request.setAttribute("tab", 1); %>
<%@ include file="/header.jsp" %>

<div id="error_message" style="display: none;">
	<fb:explanation>  
		<fb:message>Limit of characters exceeded</fb:message>
		You have to write a minimum of 1 character and a maximum of 140. 
	</fb:explanation>
</div>

 

<fb:if-is-app-user>
		<c:choose>
			<c:when test="${access}">
				<c:if test="${not grant_access}">
					<fb:prompt-permission perms="read_stream,publish_stream"> 
						Grant permission for status and stream updates 
					</fb:prompt-permission>
				</c:if>
				<c:if test="${shared}">
					<fb:success><fb:message>Shared message successfully!</fb:message> 
		  				<c:out value="${shared_message}"/>
		  				<img src='<c:out value="${img}" escapeXml="false" />'/>
		  			</fb:success>
				</c:if>
				<form action="send" method="post" onsubmit="return checkForm(this);">
					<p><h1>What's up?</h1></p>
					<textarea name="message" rows="2" cols="70"></textarea>
					<br/>
					<p><b>Status: </b><c:out value="${user.statusText}"/><input type="submit" value="Twittear"/><p>
				</form>
				<table>
				<c:forEach items="${statuses}" var="status" varStatus="rowCounter">
					<tr>
					<td>					
						<img src='<c:out value="${status.user.profileImageURL}" escapeXml="true" />'/>
					</td>
					<td>
						<b><c:out value="${status.user.name}"/></b>
						<br/> 
						<c:set var="message" value="${status.text}"/>
						<jsp:useBean id="message" type="java.lang.String" />
						<%= twitterBean.parseUrl(message) %>
						<br/>
						 <br />
						<c:set var="created" value="${status.createdAt}"/>
						<jsp:useBean id="created" type="java.util.Date" />
						<%= twitterBean.diference(created) %>
						
					</td>
					<c:if test="${not grant_access}">
						<td><a href='http://apps.facebook.com/twiteros/feed?status_id=<c:out value="${status.id}" />'>Share</a></td>
					</c:if>
					</tr>
				</c:forEach>
			</table>
			</c:when>
			<c:otherwise>
				<a href='<c:out value="${url}"/>'>Grant Access to Twiteros</a>
			</c:otherwise>
      	</c:choose>

      	
 		
	<fb:else> 
		<fb:redirect url='http://www.facebook.com/login.php?v=1.0&api_key=<c:out value="${f_key}"/>&next=http://apps.facebook.com/twiteros'/> 
	</fb:else> 
</fb:if-is-app-user>